const http = require("http");
const path = require("path");
const fs = require("fs");

/* 
跨域问题，不同的协议、ip、端口下的页面发起http请求会出现跨域问题
 */
http.createServer((req, res) => {
    if (req.url == "/") {
        var str = "";
        req.on("data", (chunk) => {
            str += chunk;
        });
        req.on("end", () => {
            console.log(str);
            var arr = str.split("\r\n");
            var index = arr.indexOf("");
            var index2 = arr.indexOf("", index + 1);
            var obj = { username: arr[index + 1], age: arr[index2 + 1] };
            console.log(obj);
            // 设置允许跨域
            res.setHeader("Access-Control-Allow-Origin", "*");
            res.writeHead(200, { "Content-Type": "text/plain" });
            res.end("received");
        });
    }
}).listen(8000);
console.log("listening 8000");
